package com.scoy.honeymei.activity.film;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.home.FilmList0Activity;
import com.scoy.honeymei.adapter.CinemaListAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.CinemaBean;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.bean.FilmListBean;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.databinding.CustomActivityList0Binding;
import com.scoy.honeymei.databinding.CustomActivityListBinding;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 影院列表
 */
public class CinemaListActivity extends BaseActivity {


    private CustomActivityList0Binding binding;
    private CinemaListActivity mContext;
    private CinemaListAdapter hAdapter;
    private int pageInt = 1;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = CustomActivityList0Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = CinemaListActivity.this;

        initNormal();

        getData(1);

    }


    @Override
    public void initNormal() {

        binding.layTop.titleTv.setText("电影院");
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        binding.layTop.titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
        binding.layTop.backIv.setOnClickListener(v -> onBackPressed());
        initRV();

        binding.norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            hAdapter.clearData();
            getData(1);

        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            getData(pageInt);
        });

    }

    private void initRV() {

        hAdapter = new CinemaListAdapter(mContext);
        binding.normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        binding.normalRv.setAdapter(hAdapter);
        hAdapter.setOnOneClick(pos -> {
            CinemaBean mBean = hAdapter.getItem(pos);
            Intent intentde = new Intent(mContext, FilmList0Activity.class);
            intentde.putExtra("cinemaId", mBean.getId());
            startActivity(intentde);
        });

    }

    private void getData(int myPage) {
        if (myPage == -1) {
            MyUtil.mytoast(mContext, "没有更多了~");
            return;
        }
        HttpParams params = new HttpParams();
        params.put("page", myPage);
        params.put("mid", 0);
        HpGo.newInstance().HttpGo(mContext, MyUrl.CINEMA_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<CinemaBean> mlist = new Gson().fromJson(o, new TypeToken<List<CinemaBean>>() {
                }.getType());
                hAdapter.addData(mlist);
                pageInt = mlist.size() == 0 ? -1 : myPage + 1;
                binding.nodataTv.setVisibility(hAdapter.getData().size() == 0 ? View.VISIBLE : View.GONE);
            }
        });


    }


}
